///////////////////////////////////////////////////////////
//  OQLCompositeCondition.cpp
//  Implementation of the Class OQLCompositeCondition
//  Created on:      23-sep-2007 21:26:15
///////////////////////////////////////////////////////////

#include "OQLCompositeCondition.h"

Cproperties OQLCompositeCondition::proper("levelCondition.properties");

OQLCompositeCondition::OQLCompositeCondition(){

}



OQLCompositeCondition::~OQLCompositeCondition(){
}

OQLCondition* OQLCompositeCondition::extractLowLevelCond(){
	
	std::map<std::string,int> map;
	
	if (proper.getSize() < 1){
		proper.CargarMapa();
	}

	std::string pkStr = proper.GetKey("OQLSimpleCondition");
	if (pkStr!=""){
		map.insert(std::make_pair("OQLSimpleCondition",atoi(pkStr.c_str())));
	}else{
		map.insert(std::make_pair("OQLSimpleCondition",1));
	}
	
	pkStr = proper.GetKey("OQLRangeCondition");
	if (pkStr!=""){
		map.insert(std::make_pair("OQLRangeCondition",atoi(pkStr.c_str())));
	}else{
		map.insert(std::make_pair("OQLRangeCondition",3));
	}
	
	pkStr = proper.GetKey("OQLOrCondition");
	if (pkStr!=""){
		map.insert(std::make_pair("OQLOrCondition",atoi(pkStr.c_str())));
	}else{
		map.insert(std::make_pair("OQLOrCondition",2));
	}
	
	pkStr = proper.GetKey("OQLAndCondition");
	if (pkStr!=""){
		map.insert(std::make_pair("OQLAndCondition",atoi(pkStr.c_str())));
	}else{
		map.insert(std::make_pair("OQLAndCondition",4));
	}
	
	int min = 5;
	OQLCondition* cond = NULL;
	for(std::vector<OQLCondition*>::iterator it = lsCondition.begin(); it != lsCondition.end(); it++)
	{
		std::map<std::string,int>::iterator iter;
		iter= map.find((*it)->getName());
		if (iter != map.end()){
			if( iter->second < min){
				min = iter->second;
				cond = (*it);
			}
		}
			
	}	
	return cond;
}

void OQLCompositeCondition::erase()
{
	for(std::vector<OQLCondition*>::iterator it = lsCondition.begin(); it != lsCondition.end(); it++)
	{
		
		if((*it) !=NULL)
		{
			(*it)->erase();
			delete (*it);
			(*it)=NULL;
		}	
	}	
	this->lsCondition.clear();
}

// cuando agrego yo libero en el destructor
void OQLCompositeCondition::addCondition(OQLCondition* condition){
	this->lsCondition.push_back(condition);
}

int OQLCompositeCondition::getSize(){
	return this->lsCondition.size();
}

OQLCondition* OQLCompositeCondition::getCondition(int off){
	
	if (off>=0 && off < (int)lsCondition.size()){
		return lsCondition[off];
	}
	return NULL;
	
}

